﻿using AutoMapper;
using Domain.IRepositorys;
using Domain.Model;
using Domain.Services;
using Domain.Types;
using DTO.Admin;
using Infrastructure.UniOfWorkBase;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Application.Admin
{
    public class TaskAdminApplication
    {
        public TaskService taskService { get; set; }

        public IEFUnitOfWork efUnitOfWork { get; set; }
        public IUserRepository userRepository { get; set; }
        public ITaskCategoryRepository taskCategoryRepository { get; set; }
        public ITaskRepository taskRepository { get; set; }

        public IList<TaskAggregate> GetTaskList(string name, TaskStatus? taskStatus, TaskAuditStatus? taskAuditStatus, int page, int size,out int totalcount)
        {
            totalcount = 0;
            var list = taskRepository.Entities;

            if (!string.IsNullOrWhiteSpace(name))
            {
                list = list.Where(i => i.Name.Contains(name));
            }
            if (taskStatus.HasValue)
            {
                list = list.Where(i => i.Status == taskStatus);
            }
            if (taskAuditStatus != null)
            {
                list = list.Where(i => i.TaskAudits.Any(u => u.AuditStatus == taskAuditStatus));
            }
            totalcount = list.Count();
            var alist= list.Skip((page - 1) * size).Take(size).ToList();


            return alist;
        }
        /// <summary>
        /// 审核权限
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="taskID"></param>
        /// <param name="taskAuditID"></param>
        /// <param name="taskAuditStatus"></param>
        /// <param name="auditReson"></param>
        /// <returns></returns>
        public bool AuditTask(int uid, int taskID, int taskAuditID, TaskAuditStatus taskAuditStatus, string auditReson)
        {
            var r = taskService.AuditTask(uid, taskID, taskAuditID, taskAuditStatus, auditReson);

            return r;
        }

        public bool EditTaskCategory(TaskCategoryInput taskCategoryInput)
        {
            TaskCategoryAggregate taskCategoryAggregate = null;

            if (taskCategoryInput.ID == 0)
            {
                taskCategoryAggregate = new TaskCategoryAggregate();
                taskCategoryAggregate.FieldData = JsonConvert.SerializeObject(taskCategoryInput.TaskFieldInfos);
                taskCategoryAggregate.Icon = taskCategoryInput.Icon;
                taskCategoryAggregate.NeedSubField = taskCategoryInput.NeedSubField;
                taskCategoryAggregate.SubTitle = taskCategoryInput.SubTitle;
                taskCategoryAggregate.Title = taskCategoryInput.Title;
                taskCategoryAggregate.NeedTaskStepData = taskCategoryInput.NeedTaskStepData;
                taskCategoryAggregate.Sort = taskCategoryInput.Sort;
                taskCategoryAggregate.IsEnable = taskCategoryInput.IsEnable;

                taskCategoryRepository.Insert(taskCategoryAggregate);
            }
            else
            {
                taskCategoryAggregate = taskCategoryRepository.GetByKey(taskCategoryInput.ID);

                taskCategoryAggregate.FieldData = JsonConvert.SerializeObject(taskCategoryInput.TaskFieldInfos);
                taskCategoryAggregate.Icon = taskCategoryInput.Icon;
                taskCategoryAggregate.NeedSubField = taskCategoryInput.NeedSubField;
                taskCategoryAggregate.SubTitle = taskCategoryInput.SubTitle;
                taskCategoryAggregate.Title = taskCategoryInput.Title;
                taskCategoryAggregate.NeedTaskStepData = taskCategoryInput.NeedTaskStepData;
                taskCategoryAggregate.Sort = taskCategoryInput.Sort;
                taskCategoryAggregate.IsEnable = taskCategoryInput.IsEnable;

                taskCategoryRepository.Update(taskCategoryAggregate);
            }

            return efUnitOfWork.Commit() > 0;
        }


        public IList<TaskCategoryOutput> GetTaskCategoryList()
        {
            var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap<TaskCategoryAggregate, TaskCategoryOutput>();

                cfg.SourceMemberNamingConvention = new PascalCaseNamingConvention();
                cfg.DestinationMemberNamingConvention = new LowerUnderscoreNamingConvention();
            });
            var mapper = config.CreateMapper();

            var list = taskCategoryRepository.Entities.Where(i => !i.IsDelete).OrderByDescending(i => i.Sort).ToList();

            return mapper.Map<IList<TaskCategoryOutput>>(list);
        }

        public bool DelTaskCategory(int cid)
        {
            var tc = taskCategoryRepository.GetByKey(cid);

            tc.IsDelete = true;


            return efUnitOfWork.Commit() > 0;
        }

        public bool EnableTaskCategory(int cid, bool enable)
        {
            var tc = taskCategoryRepository.GetByKey(cid);

            tc.IsEnable = enable;

            return efUnitOfWork.Commit() > 0;
        }
    }
}
